<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"vchao.top","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="代码经过简单的整理，已经放到Github上了。https:&#x2F;&#x2F;github.com&#x2F;zhengweichao&#x2F;Hevttc回首来看，代码质量一般，里面也有各种逻辑问题，还望各位看官海涵。接下来有时间的话，会逐步进行优化。如果对你有帮助，还望可以给个star，不胜感激。  前言在校园生活中，存在着各种各样的信息差。也正因为这些信息差的存在，经常让校园同学们面临各种各样的问题。比如丢东西的找不到东西">
<meta property="og:type" content="article">
<meta property="og:title" content="手把手带你撸一个校园APP（六）：失物招领&amp;二手交易模块">
<meta property="og:url" content="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%85%AD%EF%BC%89%EF%BC%9A%E5%A4%B1%E7%89%A9%E6%8B%9B%E9%A2%86&%E4%BA%8C%E6%89%8B%E4%BA%A4%E6%98%93%E6%A8%A1%E5%9D%97/index.html">
<meta property="og:site_name" content="乘风破浪">
<meta property="og:description" content="代码经过简单的整理，已经放到Github上了。https:&#x2F;&#x2F;github.com&#x2F;zhengweichao&#x2F;Hevttc回首来看，代码质量一般，里面也有各种逻辑问题，还望各位看官海涵。接下来有时间的话，会逐步进行优化。如果对你有帮助，还望可以给个star，不胜感激。  前言在校园生活中，存在着各种各样的信息差。也正因为这些信息差的存在，经常让校园同学们面临各种各样的问题。比如丢东西的找不到东西">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://img-blog.csdnimg.cn/20200211124046605.gif">
<meta property="article:published_time" content="2021-08-25T14:42:51.214Z">
<meta property="article:modified_time" content="2021-08-25T14:37:20.810Z">
<meta property="article:author" content="vchao">
<meta property="article:tag" content="Android">
<meta property="article:tag" content="科师有约">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/20200211124046605.gif">

<link rel="canonical" href="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%85%AD%EF%BC%89%EF%BC%9A%E5%A4%B1%E7%89%A9%E6%8B%9B%E9%A2%86&%E4%BA%8C%E6%89%8B%E4%BA%A4%E6%98%93%E6%A8%A1%E5%9D%97/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>手把手带你撸一个校园APP（六）：失物招领&二手交易模块 | 乘风破浪</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">乘风破浪</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://vchao.top/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%85%AD%EF%BC%89%EF%BC%9A%E5%A4%B1%E7%89%A9%E6%8B%9B%E9%A2%86&%E4%BA%8C%E6%89%8B%E4%BA%A4%E6%98%93%E6%A8%A1%E5%9D%97/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="vchao">
      <meta itemprop="description" content="怀揣好奇，梦想改变世界的菜鸟极客">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="乘风破浪">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          手把手带你撸一个校园APP（六）：失物招领&二手交易模块
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-08-25 22:42:51 / 修改时间：22:37:20" itemprop="dateCreated datePublished" datetime="2021-08-25T22:42:51+08:00">2021-08-25</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <blockquote>
<p>代码经过简单的整理，已经放到Github上了。<a target="_blank" rel="noopener" href="https://github.com/zhengweichao/Hevttc">https://github.com/zhengweichao/Hevttc</a><br>回首来看，代码质量一般，里面也有各种逻辑问题，还望各位看官海涵。接下来有时间的话，会逐步进行优化。如果对你有帮助，还望可以给个star，不胜感激。</p>
</blockquote>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>在校园生活中，存在着各种各样的信息差。也正因为这些信息差的存在，经常让校园同学们面临各种各样的问题。比如丢东西的找不到东西，捡到东西的找不到失主等等。其中最为突出的莫过于 【失物招领】及【二手交易】了。<br>校园作为一个比较特殊的地点，大家的素质以及诚信度普遍高于其它地方。“科师有约” 校园APP致力于减少校园信息差的存在，让校园生活更简单。</p>
<blockquote>
<p>科师有约 只是一个美好的想法，并将部分功能从技术上进行简单实现。在实际生活中并未推广开来使用，在此权当抛砖引玉之用。还是希望学弟学妹们可以做出真正的产品，并在校园生活中得以应用。</p>
</blockquote>
<p>本项目基于Bmob进行开发，很多地方需要Bmob相关知识。大家如果没了解过的话，可先自行查看 <a target="_blank" rel="noopener" href="http://doc.bmob.cn/data/android/develop_doc/">Bmob开发文档</a> 。</p>
<p><strong>系列文章：</strong></p>
<p>Github地址： <a target="_blank" rel="noopener" href="https://github.com/zhengweichao/Hevttc">科师有约校园APP</a></p>
<ol>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/80787263">手把手带你撸一个校园APP（一）：项目简介</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81456617">手把手带你撸一个校园APP（二）：应用启动和欢迎页面</a></li>
<li><a target="_blank" rel="noopener" href="https://vchao.blog.csdn.net/article/details/81837608">手把手带你撸一个校园APP（三）：用户模块（登录注册等）</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104209803">手把手带你撸一个校园APP（四）：APP框架及功能设计</a></li>
<li><a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104227390">手把手带你撸一个校园APP（五）：新闻页面中心模块</a></li>
<li>手把手带你撸一个校园APP（六）：失物招领&amp;二手交易模块</li>
<li>手把手带你撸一个校园APP（七）：图说校园模块</li>
<li>手把手带你撸一个校园APP（八）：校园通讯录模块</li>
<li>手把手带你撸一个校园APP（九）：课程表模块（模拟登陆爬取教务处课程信息）</li>
<li>手把手带你撸一个校园APP（十）：APP通用模块（更新，意见反馈等）</li>
</ol>
<h2 id="实现效果"><a href="#实现效果" class="headerlink" title="实现效果"></a>实现效果</h2><p>【失物招领】：<br>分为 寻物启事（找东西的）+ 失物招领（找失主的）<br>【二手交易】：<br>分为 淘点宝贝（想买东西的）+ 换点银子（想卖东西的）。效果图与失物招领类似</p>
<p><img src="https://img-blog.csdnimg.cn/20200211124046605.gif" alt="在这里插入图片描述"></p>
<h2 id="分析"><a href="#分析" class="headerlink" title="分析"></a>分析</h2><p>失物招领与二手交易，两个模块从整体逻辑上来说基本一致。因为我们只做简易实现，所以就两个模块就不再区分讲解（下简称 “需求” ）。<br>（当然，这里还可以做很多很多优化，本文后面【未来可做的优化】部分会进一步讲解）<br>需要实现功能如下：</p>
<ol>
<li>发布需求（含 图片压缩及上传）</li>
<li>需求列表展示</li>
<li>需求详情展示</li>
<li>沟通联系（简化为 拨打电话）</li>
</ol>
<p>其中需求列表的展示页面，与 新闻中心模块 使用的技术代码基本相同。想了解的可以再退回去看 <a target="_blank" rel="noopener" href="https://blog.csdn.net/zheng_weichao/article/details/104227390">上篇文章</a>。 </p>
<h2 id="数据库设计"><a href="#数据库设计" class="headerlink" title="数据库设计"></a>数据库设计</h2><p>（因两模块基本逻辑相似，仅就失物招领模块开讲）<br>为了使数据区分的更清楚，设计数据库时，将 “失物招领” 与 “寻物启事” 分别设置为两个表。其中的字段名称设计等均相同。字段如下：</p>
<table>
<thead>
<tr>
<th>字段名</th>
<th>描述</th>
<th>类型</th>
<th>是否主键</th>
</tr>
</thead>
<tbody><tr>
<td>objectId</td>
<td>事件id号</td>
<td>String</td>
<td>是</td>
</tr>
<tr>
<td>title</td>
<td>事件标题</td>
<td>String</td>
<td>-</td>
</tr>
<tr>
<td>tel</td>
<td>联系方式</td>
<td>Number</td>
<td>-</td>
</tr>
<tr>
<td>author</td>
<td>发布人</td>
<td>String</td>
<td>-</td>
</tr>
<tr>
<td>content</td>
<td>事件详细描述</td>
<td>String</td>
<td>-</td>
</tr>
<tr>
<td>isOver</td>
<td>事件处理状态</td>
<td>Boolean</td>
<td>-</td>
</tr>
<tr>
<td>createdAt</td>
<td>事件发布时间</td>
<td>Date</td>
<td>-</td>
</tr>
</tbody></table>
<p>（大家也可以设置为一个表，其中另外加一个字段进行区分） </p>
<h2 id="安卓实现"><a href="#安卓实现" class="headerlink" title="安卓实现"></a>安卓实现</h2><h3 id="发布需求"><a href="#发布需求" class="headerlink" title="发布需求"></a>发布需求</h3><p>此处应用的技术都比较简单。发布需求 其实就是简单的数据库的增加操作。带图片的需求需要再做一下进一步的处理（图片压缩以及上传）</p>
<h4 id="发布无图片的需求"><a href="#发布无图片的需求" class="headerlink" title="发布无图片的需求"></a>发布无图片的需求</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">LoseItem bean = <span class="keyword">new</span> LoseItem();</span><br><span class="line">MyUser user = BmobUser.getCurrentUser(MyUser.class);</span><br><span class="line">bean.setAuthor(user.getUsername());</span><br><span class="line">bean.setContent(loseDesc);</span><br><span class="line">bean.setTel(loseTel);</span><br><span class="line">bean.setTitle(loseTitle);</span><br><span class="line">bean.save(<span class="keyword">new</span> SaveListener&lt;String&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">done</span><span class="params">(String objectId, BmobException e)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (e == <span class="keyword">null</span>) &#123;</span><br><span class="line">            ToastUtil.show(LoseAddActivity.<span class="keyword">this</span>, <span class="string">&quot;发布成功&quot;</span>, Toast.LENGTH_SHORT);</span><br><span class="line">            LoadDialog.dismiss(LoseAddActivity.<span class="keyword">this</span>);</span><br><span class="line">            startActivity(<span class="keyword">new</span> Intent(LoseAddActivity.<span class="keyword">this</span>, LoseActivity.class));</span><br><span class="line">            finish();</span><br><span class="line">        &#125; <span class="keyword">else</span> &#123;</span><br><span class="line">            ToastUtil.show(LoseAddActivity.<span class="keyword">this</span>, <span class="string">&quot;发布失败，请稍后再试&quot;</span>, Toast.LENGTH_SHORT);</span><br><span class="line">            LoadDialog.dismiss(LoseAddActivity.<span class="keyword">this</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<h4 id="发布有图片的需求"><a href="#发布有图片的需求" class="headerlink" title="发布有图片的需求"></a>发布有图片的需求</h4><p>现在手机像素一般都很高，所以一个图片好几MB 的情况也很常见。直接上传的话，会极大浪费用户流量以及文件存储空间。所以需要对图片进行压缩处理后再上传。<br>所以发布有图片的需求需要拆分为以下 4 步来进行。</p>
<p><strong>1. 选择图片</strong><br>选择图片部分使用了 廖子尧 大神的 imagepicker 库。完全仿微信UI，实现了拍照、图片选择（单选/多选）、 裁剪 、旋转 等功能。</p>
<blockquote>
<p>Github : <a target="_blank" rel="noopener" href="https://github.com/jeasonlzy/ImagePicker">imagepicker</a><br>作者：廖子尧  jeasonlzy</p>
</blockquote>
<p>大神的文档写的十分详尽，集成方法我就不再赘述了。核心代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 设置 图片选择器 相应参数</span></span><br><span class="line">ImagePicker imagePicker = ImagePicker.getInstance();</span><br><span class="line">imagePicker.setImageLoader(<span class="keyword">new</span> GlideImageLoader());   <span class="comment">//设置图片加载器</span></span><br><span class="line">imagePicker.setShowCamera(<span class="keyword">true</span>);                      <span class="comment">//显示拍照按钮</span></span><br><span class="line">imagePicker.setCrop(<span class="keyword">true</span>);                           <span class="comment">//允许裁剪（单选才有效）</span></span><br><span class="line">imagePicker.setSaveRectangle(<span class="keyword">true</span>);                   <span class="comment">//是否按矩形区域保存</span></span><br><span class="line">imagePicker.setSelectLimit(maxImgCount);              <span class="comment">//选中数量限制</span></span><br><span class="line">imagePicker.setStyle(CropImageView.Style.RECTANGLE);  <span class="comment">//裁剪框的形状</span></span><br><span class="line">imagePicker.setFocusWidth(<span class="number">800</span>);                       <span class="comment">//裁剪框的宽度。单位像素（圆形自动取宽高最小值）</span></span><br><span class="line">imagePicker.setFocusHeight(<span class="number">800</span>);                      <span class="comment">//裁剪框的高度。单位像素（圆形自动取宽高最小值）</span></span><br><span class="line">imagePicker.setOutPutX(<span class="number">1000</span>);                         <span class="comment">//保存文件的宽度。单位像素</span></span><br><span class="line">imagePicker.setOutPutY(<span class="number">1000</span>);                         <span class="comment">//保存文件的高度。单位像素</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// ...省略其它代码</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 开始选择图片</span></span><br><span class="line">List&lt;String&gt; names = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">names.add(<span class="string">&quot;拍照&quot;</span>);</span><br><span class="line">names.add(<span class="string">&quot;相册&quot;</span>);</span><br><span class="line">showDialog(<span class="keyword">new</span> SelectDialog.SelectDialogListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onItemClick</span><span class="params">(AdapterView&lt;?&gt; parent, View view, <span class="keyword">int</span> position, <span class="keyword">long</span> id)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">switch</span> (position) &#123;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">0</span>:</span><br><span class="line">                <span class="comment">// 直接调起相机</span></span><br><span class="line">                ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());</span><br><span class="line">                Intent intent = <span class="keyword">new</span> Intent(LoseAddActivity.<span class="keyword">this</span>, ImageGridActivity.class);</span><br><span class="line">                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, <span class="keyword">true</span>); <span class="comment">// 是否是直接打开相机</span></span><br><span class="line">                startActivityForResult(intent, REQUEST_CODE_SELECT);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">            <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">                <span class="comment">//打开选择,本次允许选择的数量</span></span><br><span class="line">                ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size());</span><br><span class="line">                Intent intent1 = <span class="keyword">new</span> Intent(LoseAddActivity.<span class="keyword">this</span>, ImageGridActivity.class);</span><br><span class="line">                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES, images);</span><br><span class="line">                startActivityForResult(intent1, REQUEST_CODE_SELECT);</span><br><span class="line">                <span class="keyword">break</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;, names);</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p><strong>2. 图片压缩</strong><br>图片压缩部分采用了 Luban（鲁班） —— Android图片压缩工具，仿微信朋友圈压缩策略。<br>作者采用逆向推算，效果已经很接近微信朋友圈压缩后的效果。<br>（再次踩在了巨人的肩膀上……）</p>
<blockquote>
<p>Github :  <a target="_blank" rel="noopener" href="https://github.com/Curzibn/Luban">Luban（鲁班）</a><br>作者： Curzibn</p>
</blockquote>
<p>核心代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">Luban.with(LoseAddActivity.<span class="keyword">this</span>)</span><br><span class="line">     .load(paths)                                   <span class="comment">// 传入要压缩的图片列表</span></span><br><span class="line">     .ignoreBy(<span class="number">100</span>)                                  <span class="comment">// 忽略不压缩图片的大小</span></span><br><span class="line">     .setTargetDir(getPath())                        <span class="comment">// 设置压缩后文件存储位置</span></span><br><span class="line">     .setCompressListener(<span class="keyword">new</span> OnCompressListener() &#123; <span class="comment">//设置回调</span></span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onStart</span><span class="params">()</span> </span>&#123;</span><br><span class="line">             <span class="comment">// 开始压缩 ...</span></span><br><span class="line">         &#125;</span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSuccess</span><span class="params">(File file)</span> </span>&#123;</span><br><span class="line">             <span class="comment">// 压缩成功 ...</span></span><br><span class="line">         &#125;</span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Throwable e)</span> </span>&#123;</span><br><span class="line">             <span class="comment">// 当压缩过程出现问题时调用</span></span><br><span class="line">         &#125;</span><br><span class="line">      &#125;).launch();    <span class="comment">//启动压缩</span></span><br></pre></td></tr></table></figure>


<p><strong>3. 图片上传</strong><br>这里是按照Bmob官方代码来写的。详情可见 <a target="_blank" rel="noopener" href="http://doc.bmob.cn/data/android/develop_doc/#8">Bmob文件管理</a> 。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">BmobFile.uploadBatch(filePaths, <span class="keyword">new</span> UploadBatchListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onSuccess</span><span class="params">(List&lt;BmobFile&gt; files, List&lt;String&gt; urls)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 上传图片成功</span></span><br><span class="line">        <span class="comment">//1、files-上传完成后的BmobFile集合，是为了方便大家对其上传后的数据进行操作，例如你可以将该文件保存到表中</span></span><br><span class="line">        <span class="comment">//2、urls-上传文件的完整url地址</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(<span class="keyword">int</span> statuscode, String errormsg)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// 上传图片失败</span></span><br><span class="line">        LoadDialog.dismiss(LoseAddActivity.<span class="keyword">this</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onProgress</span><span class="params">(<span class="keyword">int</span> curIndex, <span class="keyword">int</span> curPercent, <span class="keyword">int</span> total, <span class="keyword">int</span> totalPercent)</span> </span>&#123;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>需要注意的是 <strong>上传多张图片，onSuccess 会多次调用</strong> 。别忘了做相应处理。</p>
<p><strong>4. 发布需求</strong><br>和发布无图片需求代码基本一致，只不过多了一句设置图片的代码。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">LoseItem bean = <span class="keyword">new</span> LoseItem();</span><br><span class="line"><span class="comment">// ... 省略其它代码</span></span><br><span class="line">bean.setPic(files.get(<span class="number">0</span>));</span><br><span class="line"></span><br><span class="line">bean.save(<span class="keyword">new</span> SaveListener&lt;String&gt;() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">done</span><span class="params">(String objectId, BmobException e)</span> </span>&#123;</span><br><span class="line">        <span class="comment">// ... 省略其它代码</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>


<p>至此，失物招领 &amp; 二手交易模块基本完成。</p>
<p>然而整个流程中还有很多可以优化的地方，作者大概率没有时间和精力去做这部分了。<br>特将这部分内容整理如下，若他人有类似需求，以望能有所启迪或帮助。</p>
<h2 id="未来可做的优化"><a href="#未来可做的优化" class="headerlink" title="未来可做的优化"></a>未来可做的优化</h2><p>简而言之，就是 <strong>提升平台能力</strong>。</p>
<p>因两个模块逻辑相似性较高，故此处以【失物招领】为例：<br>在一个完整的过程中，总共存在3方：丢东西的，捡东西的，平台。</p>
<p>本文上面已经讲解了最基础简易的沟通流程：</p>
<ol>
<li>丢东西的 在平台发布 寻物启事；捡东西的看见 –&gt; 私下联系；</li>
<li>捡东西的 在平台发布 失物招领；丢东西的看见 –&gt; 私下联系；</li>
</ol>
<p>这种方式固然简单粗暴，但是显得不太灵活。<br>身为平台，应该发挥出平台更大的能力。那么应该从哪里开始入手呢？</p>
<p>校园是一个很特殊的地方，这里的失物招领有许多特殊之处，总结一下不难发现：</p>
<ol>
<li>用户群体（学生，教职工，家属楼住户）绝大多数都接受过高等教育，文化素质较高，认知较为统一； ==&gt; 【其它用户捡到后，有较大概率归还】</li>
<li>用户活动范围较为一致，活动区域较为封闭； ==&gt;【用户丢失物品，较大概率被其它校园用户捡到】</li>
<li>用户捡到或丢失的物品有较高重复性（校园卡，身份证，课本）； ==&gt; 【物品上一般带有 失主信息，可以通过实名信息直接锁定失主】</li>
<li>根据院系，专业，班级等，很容易找到相关实名用户。 ==&gt; 【根据附属信息，很容易找到某人或其朋友同学】</li>
<li>有很多人捡到东西后，会交给专门的组织或个人（失物招领协会，门卫，校园卡服务中心等）。 ==&gt; 【可以集中起来这些地方的信息，统一处理】</li>
</ol>
<p>综上所述，未来如果有学弟学妹想做这部分，按个人看法，可做的优化如下：</p>
<ol>
<li>加强用户实名认证操作，仅限实名认证用户使用APP。</li>
<li>定期收集整理线下失物招领信息，录入线上系统。</li>
<li>线上系统运用实名信息匹配失主并通过推送私信等方式进行联系（保证用户隐私的前提下进行）。</li>
<li>对线上信息进行匹配以及相似内容匹配操作，以优化处理双方都发布了信息的情况。</li>
</ol>
<hr>
<p>至此，本文内容全部完毕，项目中大量依赖或采用了第三方项目的代码。在此向各位有分享精神的大佬们致敬！</p>
<p>如果本文对你有所帮助，还望可以随手赏一个点赞哈 ~ ~</p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
              <a href="/tags/%E7%A7%91%E5%B8%88%E6%9C%89%E7%BA%A6/" rel="tag"># 科师有约</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%85%AB%EF%BC%89%EF%BC%9A%E6%A0%A1%E5%9B%AD%E9%80%9A%E8%AE%AF%E5%BD%95%E6%A8%A1%E5%9D%97/" rel="prev" title="手把手带你撸一个校园APP（八）：校园通讯录模块">
      <i class="fa fa-chevron-left"></i> 手把手带你撸一个校园APP（八）：校园通讯录模块
    </a></div>
      <div class="post-nav-item">
    <a href="/2021/08/25/%E6%89%8B%E6%8A%8A%E6%89%8B%E5%B8%A6%E4%BD%A0%E6%92%B8%E4%B8%80%E4%B8%AA%E6%A0%A1%E5%9B%ADAPP%EF%BC%88%E5%9B%9B%EF%BC%89%EF%BC%9AAPP%E5%8A%9F%E8%83%BD%E8%AE%BE%E8%AE%A1%E5%8F%8A%E4%B8%BB%E9%A1%B5%E9%9D%A2%E6%A1%86%E6%9E%B6/" rel="next" title="手把手带你撸一个校园APP（四）：APP框架及功能设计">
      手把手带你撸一个校园APP（四）：APP框架及功能设计 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%95%88%E6%9E%9C"><span class="nav-number">2.</span> <span class="nav-text">实现效果</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E6%9E%90"><span class="nav-number">3.</span> <span class="nav-text">分析</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%95%B0%E6%8D%AE%E5%BA%93%E8%AE%BE%E8%AE%A1"><span class="nav-number">4.</span> <span class="nav-text">数据库设计</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AE%89%E5%8D%93%E5%AE%9E%E7%8E%B0"><span class="nav-number">5.</span> <span class="nav-text">安卓实现</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E9%9C%80%E6%B1%82"><span class="nav-number">5.1.</span> <span class="nav-text">发布需求</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E6%97%A0%E5%9B%BE%E7%89%87%E7%9A%84%E9%9C%80%E6%B1%82"><span class="nav-number">5.1.1.</span> <span class="nav-text">发布无图片的需求</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%8F%91%E5%B8%83%E6%9C%89%E5%9B%BE%E7%89%87%E7%9A%84%E9%9C%80%E6%B1%82"><span class="nav-number">5.1.2.</span> <span class="nav-text">发布有图片的需求</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9C%AA%E6%9D%A5%E5%8F%AF%E5%81%9A%E7%9A%84%E4%BC%98%E5%8C%96"><span class="nav-number">6.</span> <span class="nav-text">未来可做的优化</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">vchao</p>
  <div class="site-description" itemprop="description">怀揣好奇，梦想改变世界的菜鸟极客</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">46</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
        <span class="site-state-item-count">4</span>
        <span class="site-state-item-name">分类</span>
      </div>
      <div class="site-state-item site-state-tags">
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">标签</span>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        
  <div class="beian"><a href="https://beian.miit.gov.cn/" rel="noopener" target="_blank">冀ICP备 17022751号-1 </a>
      <img src="https://beian.miit.gov.cn" style="display: inline-block;">
  </div>

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vchao</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  















  

  

</body>
</html>
